﻿var JobGrid = JobGrid || {};
JobGrid = function () {
    var gridInEditMode = false;
    var scratchDropDownChanged = false;
    var jobDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/AddJobTitle/GetJobTitles",
                type: "get",
                dataType: "json"
            },


            create: {

                url: "/AddJobTitle/CreateAddJob",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"

            },
            update: {
                url: "/AddJobTitle/UpdateJob",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"
            },
            destroy: {
                url: "/AddJobTitle/DeleteJob",
                type: "POST",
                dataType: "json",
                contentType: "application/JSON"
            },
            parameterMap: function (options) {
                return kendo.stringify(options); // kendo.stringify serializes to JSON string
            }

        },

        pageSize: 5,
        schema: {
            model: {
                id: "id",
                fields: {
                    id: {
                        editable: false, nullable: true,
                        type: "int"
                    },
                    code: {
                        type: "string",
                        validation: { required: true }
                    },
                    description: {

                        type: "string",
                        validation: { required: true }
                    },

                    error: function (a) {
                        $('#jobGrid').data("kendoGrid").cancelChanges();
                    },

                }
            }
        }

    });

    return {
        jobDataSource: jobDataSource,
        gridInEditMode: gridInEditMode,
        scratchDropDownChanged: scratchDropDownChanged
    }
}();




$(function () {

    $(window).on("beforeunload", function () {

        if (JobGrid.gridInEditMode) {

            return "Are you sure you want to discard all your changes."

        }

    });
    $("#JobGrid").kendoGrid({
        dataSource: JobGrid.jobDataSource,
        scrollable: true,
        height: "250px",
        toolbar: ["create"], //, "save", "cancel"],
        pageable: true,
        height: 300,
        columns: [
     // { field: "LogsDate", title: "LogsDate*", filterable: false },
      { field: "code", title: "Code", filterable: false, width: "8em" },
      { field: "description", title: "Description", width: "8em" },

      {
          title: "&nbsp;",
          width: "25em",
          //command: ["edit", "destroy"]
          command: [

              "edit",
              {
                  name: "Delete",


                  click: function (e) {
                      debugger;
                      var tr = $(e.target).closest("tr"); //get the row for deletion
                      var dataItem = this.dataItem(tr); //get the row data so it can be referred later
                      //see what is coming in dataItem
                      //if (dataItem > 0) {
                      //    $("#delValidation").addClass('in')
                      //}
                      //else {

                      deleteTopicConfirmation(e, this);
                      ////}
                      return false;
                  }
              }
          ]
      }
        ],
        filterable: {
            extra: false,
            operators: {
                string: {
                    eq: "Is equal to",
                    neq: "Is not equal to"
                }
            }
        },

        editable: "popup",
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        selectable: true,
        // change: onTopicChange,
        // dataBound: onTopicsGridDataBound,

        cancel: function () {
            JobGrid.gridInEditMode = false;
        }
    });

    JobGrid.jobDataSource.bind("requestEnd", showSuccessMessage);


    function showSuccessMessage(e) {
        if (e.type === "update" || e.type === "create") {
            JobGrid.gridInEditMode = false;
            $("#saveConfirm").addClass('in');
            e.sender.read();
        }
    }


    $(".close").click(function () {
        $(this).parent().removeClass('in');
    });


    function deleteTopicConfirmation(e, widget) {
        var grid = $('#JobGrid').data("kendoGrid");
        deleteConfirmation(e, widget, grid);
    }

    function deleteConfirmation(e, widget, grid) {

        var currentRow = $(e.target).closest("tr"); //get the row for deletion

        var currentRowData = widget.dataItem(currentRow); //get the row data so it can be  referred later

        var kendoWindow = $("<div />").kendoWindow({

            title: "Confirmation",
            resizable: true,
            modal: true,
            width: "400px"
        });

        kendoWindow.data("kendoWindow")

            .content($("#delete-confirmation").html())

            .center().open();

        kendoWindow

            .find(".delete-confirm,.delete-cancel")
            .click(function () {
                if ($(this).hasClass("delete-confirm")) {

                    grid.dataSource.remove(currentRowData);

                    grid.dataSource.sync();

                }

                $(this).closest("[data-role=window]").kendoWindow("close");

            });

    }

});